package com.woniu.tools;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

@Service
public class WriteFileAndContent {
    // 修改重复的writeContent名称
    @Tool(description = "创建文件，写入，内容", name = "fileWriter")
    public String wrightContent(String filePath,String content) {
        filePath = filePath.replaceAll("'", "");
        content = content.replaceAll("'", "");
        return  filePathInsert(filePath,content);
    }

    private String filePathInsert(String filePath, String content) {
        //创建文件
        File file = new File(filePath);
        if (!file.exists()) {
            //创建文件
            try {
                file.createNewFile();
                System.out.println("文件创建成功");
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        //写入内容
        try {
            FileWriter fileWriter = new FileWriter(file);
            fileWriter.write(content);
            fileWriter.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "ok";
    }
}